package com.zjeav.lingjiao.base.tools;

import com.zjeav.lingjiao.base.baseBean.ItemResource;
import com.zjeav.lingjiao.base.baseBean.LocalMetadata;
import com.zjeav.lingjiao.base.baseBean.Metadata;
import com.zjeav.lingjiao.base.config.BaseConfig;
import com.zjeav.lingjiao.base.response.BookDetailResponse;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * Created by kekezhou on 2018-10-29.
 */
public class LocalUtils {
    public static void SaveMete(DbManager db,ItemResource resource, String metaName, String lrcName,BookDetailResponse bookDetailResponse){
        LocalMetadata metadata=new LocalMetadata();
        metadata.setId(resource.getId());
        metadata.setBookid(bookDetailResponse.getId());
        metadata.setUrl(BaseConfig.FILE_DOWNLOAD + metaName);
        metadata.setSrturl(BaseConfig.FILE_DOWNLOAD + lrcName);
        metadata.setCover(resource.getMetadata().getCover());
        metadata.setFavorites(resource.isFavorites());
//        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
        metadata.setFavoritesTime("");
        metadata.setBookName(bookDetailResponse.getTitle());
        metadata.setDuration(resource.getMetadata().getDuration());
        metadata.setTitle(resource.getTitle());
        try {
           db.saveBindingId(metadata);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static void SaveMete(DbManager db, Metadata resource, String metaName, String lrcName, int bookid){
        LocalMetadata metadata=new LocalMetadata();
        metadata.setId(resource.getId());
        metadata.setBookid(bookid);
        metadata.setUrl(BaseConfig.FILE_DOWNLOAD + metaName);
        metadata.setSrturl(BaseConfig.FILE_DOWNLOAD + lrcName);
        metadata.setCover(resource.getCover());
        metadata.setFavorites(true);
//        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
        metadata.setFavoritesTime(resource.getFavoritesTime());
        metadata.setBookName(resource.getBookName());
        metadata.setDuration(resource.getDuration());
        metadata.setTitle(resource.getTitle());
        try {
            db.saveBindingId(metadata);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static ArrayList<LocalMetadata> getList(DbManager db,int bookid)  {
        try {
            return (ArrayList<LocalMetadata>) db.selector(LocalMetadata.class).where("bookid","=",bookid).findAll();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void deleItem(DbManager db ,int id){
        try {
            db.delete(LocalMetadata.class, WhereBuilder.b("id","=",id));
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
